#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义结构体
struct Worker {
	char name[24];
	int age;
	double salary;
};

// 直接取名字
struct {
	int min;
	int max;
} num1={ 10,100},num2;

struct date {
 int year;
 int month;
 int day;
};

struct student {
	int number;
	int sex;
	int age;
	char name[10];
	struct date brithday;
};

// 结构体别名，同时结构体指针的别名也是Room
typedef struct Room {
	char name[12];
	int area;
} Room;

// 结构体Room，取一个名字为Room_的别名，也允许取多个别名
typedef Room Room_,Room__;
// 结构体指针进行取别名
typedef Room__* Roomer;

int main()
{
	// 1.声明结构体，并初始化
	printf("-----结构体声明----\n");
	struct Worker worker = {"WU KAI JIE",28,1000};
	printf("worker.name = %s, worker.age = %d, worker.salary = %.2f\n",worker.name,worker.age,worker.salary);
	num2.min = 13;
	num2.max = 88;
	printf("num1.min = %d, num1.max = %d, num2.min = %d, num2.max = %d\n",num1.min,num1.max,num2.min,num2.max);
	
	// 2.结构体指针和动态内存开辟
	printf("-----结构体指针和动态内存开辟-----\n");
	struct Worker* worker_p = &worker; 	// 获取结构体指针
	worker_p->age = 16;					// 通过结构体指针取修改变量
	strcpy(worker_p->name,"Tom");		// 通过拷贝的方式对name赋值	
	printf("worker.name = %s, worker.age = %d, worker.salary = %.2f\n",worker.name,worker.age,worker.salary);

	// 开辟内存的方式初始化结构体指针
	struct Worker* worker_m = (struct Worker*)malloc(sizeof(struct Worker));
	worker_m->age = 66;
	strcpy(worker_m->name,"old boy");
	printf("worker_m->age:%d,worker_m->name:%s\n",worker_m->age,worker_m->name);

	// 3.结构体指针数组
	struct Worker workers[10] = {{"a",1,2},{"b",3,4},{"c",5,6}}; //创建10个Worker，静态开辟，对前三个进行初始化
	printf("workers[0].name = %s, workers[0].age = %d, workers[0].salary = %.2f\n",workers[0].name,workers[0].age,workers[0].salary);
	// 动态开辟结构体指针数组内存
	struct Worker* workker = (struct Worker*)malloc(sizeof(struct Worker)*10); //开辟10个Worker结构体空间
	// 对数组中第一个结构体进行赋值
	workker->age = 55;
	strcpy(workker->name,"buff");
	printf("age:%d, name:%s\n",workker->age,workker->name);
	workker +=9; // 指针移动到元素的最后一位
	workker->age = 32;
	strcpy(workker->name,"buff2");
	printf("age:%d, name:%s\n",workker->age,workker->name);

	// 计算结构体的占用空间的大小
	/*
	 * 计算方式：
	 * 1.按照最大类型字节数去计算
	 * 2.计算的时候只会按照基本数据类型去计算
	 * 3.计算的时候，首先会把所有数据类型进行相加得到total，然后对最大数据类型进行整除，如果除不尽，就是total进行++，直到除尽为止，此时total就是结构体占用字节数
	 * 4.驱动模型？
	 * */
	int size = sizeof(struct student);
	printf("struct student size: %d\n",size);

	// 4.结构体和结构体指针取别名
	struct Room* room = (struct Room*)malloc(sizeof(struct Room)*5);	
	printf("struct Room size: %ld\n",sizeof(room));

	exit(0);
}

